package action;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import model.Detail;
import model.Dish;
import model.Order;
import net.sf.json.JSONArray;
import service.DetailService;
import service.DishService;
import service.OrderService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2016/8/15.
 */

public class OrderAction extends ActionSupport {
    private DishService dishService;
    private OrderService orderService;
    private JSONArray result;
    private DetailService detailService;
    private Dish dish;
    private Detail detail;
    private String cate;
    private int receive;

    public Dish getDish() {
        return dish;
    }

    public void setDish(Dish dish) {
        this.dish = dish;
    }

    public Detail getDetail() {
        return detail;
    }

    public void setDetail(Detail detail) {
        this.detail = detail;
    }

    protected Map getSession(){
        return ActionContext.getContext().getSession();
    }

    protected void saveData(String key, Object value){
        getSession().put(key, value);
    }

    protected Object getData(String key){
        return getSession().get(key);
    }

    protected void removeData(String key){
        getSession().remove(key);
    }

    protected Integer getId(){
        try {
            int id = (int) getData("order");
            return id;
        }catch (Exception e){
            return null;
        }
    }

    public String initMenu(){
        List<Dish> dishList = dishService.findAll();
        result = JSONArray.fromObject(dishList);
        return "result";
    }


    public String addDish(){
        List<Detail> detailList = detailService.findByOrder(getId());
        for(Detail d : detailList){
            if(d.getDishId() == dish.getId() && d.getCate().equals(cate)){
                d.setAmount(d.getAmount() + 1);
                detailService.update(d);
                saveData("detailList", detailService.findByOrder(getId()));
                return SUCCESS;
            }
        }
        Detail d = new Detail();
        d.setDishId(dish.getId());
        d.setCate(cate);
        d.setAmount(1);
        d.setOrderId(getId());
        orderService.newDetail(d);
        saveData("detailList", detailService.findByOrder(getId()));
        return SUCCESS;
    }

    public String removeDish(){
        List<Detail> detailList = detailService.findByOrder(getId());
        for(Detail d : detailList){
            if(d.getDishId().equals(detail.getDishId()) && d.getCate().equals(detail.getCate())){
                d.setAmount(d.getAmount() - 1);
                if(d.getAmount() == 0)
                    detailService.delete(d);
                else
                    detailService.update(d);
            }
        }
        saveData("detailList", detailService.findByOrder(getId()));
        return SUCCESS;
    }

    public String cancelOrder(){
        removeData("detailList");
        return SUCCESS;
    }

    public String getCate() {
        return cate;
    }

    public void setCate(String cate) {
        this.cate = cate;
    }

    public int getReceive() {
        return receive;
    }

    public void setReceive(int receive) {
        this.receive = receive;
    }

    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    public void setDishService(DishService dishService) {
        this.dishService = dishService;
    }

    public void setDetailService(DetailService detailService) {
        this.detailService = detailService;
    }

    public JSONArray getResult(){ return this.result; }
}